Trò chơi Angry Birds trong UNITY Engine
31.663 lượt xem;
- PositionPathTweenProperty.cs
- specificTypes /
- properties /
- GoKit /
- Plugins /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4
5 /// <summary>
6 /// tweens position along a path at constant speed between nodes. isRelative makes the path movement
7 /// relative to the start position of the object. a "from" tween will reverse the path and make the start
8 /// position be the last node in the path.
9 /// </summary>
10 public class PositionPathTweenProperty : AbstractTweenProperty
11 {
12 protected bool _useLocalPosition;
13 public bool useLocalPosition { get { return _useLocalPosition; } }
14
15 protected Transform _target;
16 protected Vector3 _startValue;
17
18 private GoSpline _path;
19 private GoLookAtType _lookAtType = GoLookAtType.None;
20 private Transform _lookTarget;
21 private GoSmoothedQuaternion _smoothedRotation;
22
23
24 public PositionPathTweenProperty( GoSpline path, bool isRelative = false, bool useLocalPosition = false, GoLookAtType lookAtType = GoLookAtType.None, Transform lookTarget = null ) : base( isRelative )
25 {
26 _path = path;
27 _useLocalPosition = useLocalPosition;
28 _lookAtType = lookAtType;
29 _lookTarget = lookTarget;
30 }
31
32
33 #region Object overrides
34
35 public override int GetHashCode()
36 {
37 return base.GetHashCode();
38 }
39
40
41 public override bool Equals( object obj )
42 {
43 // start with a base check and then compare if we are both using local values
44 if( base.Equals( obj ) )
45 return this._useLocalPosition == ((PositionPathTweenProperty)obj)._useLocalPosition;
46
47 // if we get here, we need to see if the other object is a position tween of the same kind
48 var otherAsPosition = obj as PositionTweenProperty;
49 if( otherAsPosition != null )
50 return this._useLocalPosition == otherAsPosition.useLocalPosition;
51
52 return false;
53 }
54
55 #endregion
56
57
58 public override void prepareForUse()
59 {
60 _target = _ownerTween.target as Transform;
61
62 // if this is a from tween first reverse the path then build it
63 if( _ownerTween.isFrom )
64 _path.reverseNodes();
65 else
66 _path.unreverseNodes();
67
68 _path.buildPath();
69
70 // a from tween means the start value is the last node
71 if( _ownerTween.isFrom )
72 {
73 _startValue = _path.getLastNode();
74 }
75 else
76 {
77 if( _useLocalPosition )
78 _startValue = _target.localPosition;
79 else
80 _startValue = _target.position;
81 }
82
83 // validate the lookTarget if we are set to look at it
84 if( _lookAtType == GoLookAtType.TargetTransform )
85 {
86 if( _lookTarget == null )
87 _lookAtType = GoLookAtType.None;
88 }
89
90 // prep our smoothed rotation
91 _smoothedRotation = _target.rotation;
92 }
93
94
95 public override void tick( float totalElapsedTime )
96 {
97 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
98 var vec = _path.getPointOnPath( easedTime );
99
100 // if we are relative, add the vec to our startValue
101 if( _isRelative )
102 vec += _startValue;
103
104
105 // handle look types
106 switch( _lookAtType )
107 {
108 case GoLookAtType.NextPathNode:
109 {
110 _smoothedRotation.smoothValue = vec.Equals( _target.position ) ? Quaternion.identity : Quaternion.LookRotation( vec - _target.position );
111 _target.rotation = _smoothedRotation.smoothValue;
112 //var lookAtNode = ( _ownerTween.isReversed || _ownerTween.isLoopoingBackOnPingPong ) ? _path.getPreviousNode() : _path.getNextNode();
113 //_target.LookAt( lookAtNode, Vector3.up );
114 break;
115 }
116 case GoLookAtType.TargetTransform:
117 {
118 _target.LookAt( _lookTarget, Vector3.up );
119 break;
120 }
121 }
122
123
124 // assign the position
125 if( _useLocalPosition )
126 _target.localPosition = vec;
127 else
128 _target.position = vec;
129 }
130
131 }
2 using System.Collections;
3
4
5 /// <summary>
6 /// tweens position along a path at constant speed between nodes. isRelative makes the path movement
7 /// relative to the start position of the object. a "from" tween will reverse the path and make the start
8 /// position be the last node in the path.
9 /// </summary>
10 public class PositionPathTweenProperty : AbstractTweenProperty
11 {
12 protected bool _useLocalPosition;
13 public bool useLocalPosition { get { return _useLocalPosition; } }
14
15 protected Transform _target;
16 protected Vector3 _startValue;
17
18 private GoSpline _path;
19 private GoLookAtType _lookAtType = GoLookAtType.None;
20 private Transform _lookTarget;
21 private GoSmoothedQuaternion _smoothedRotation;
22
23
24 public PositionPathTweenProperty( GoSpline path, bool isRelative = false, bool useLocalPosition = false, GoLookAtType lookAtType = GoLookAtType.None, Transform lookTarget = null ) : base( isRelative )
25 {
26 _path = path;
27 _useLocalPosition = useLocalPosition;
28 _lookAtType = lookAtType;
29 _lookTarget = lookTarget;
30 }
31
32
33 #region Object overrides
34
35 public override int GetHashCode()
36 {
37 return base.GetHashCode();
38 }
39
40
41 public override bool Equals( object obj )
42 {
43 // start with a base check and then compare if we are both using local values
44 if( base.Equals( obj ) )
45 return this._useLocalPosition == ((PositionPathTweenProperty)obj)._useLocalPosition;
46
47 // if we get here, we need to see if the other object is a position tween of the same kind
48 var otherAsPosition = obj as PositionTweenProperty;
49 if( otherAsPosition != null )
50 return this._useLocalPosition == otherAsPosition.useLocalPosition;
51
52 return false;
53 }
54
55 #endregion
56
57
58 public override void prepareForUse()
59 {
60 _target = _ownerTween.target as Transform;
61
62 // if this is a from tween first reverse the path then build it
63 if( _ownerTween.isFrom )
64 _path.reverseNodes();
65 else
66 _path.unreverseNodes();
67
68 _path.buildPath();
69
70 // a from tween means the start value is the last node
71 if( _ownerTween.isFrom )
72 {
73 _startValue = _path.getLastNode();
74 }
75 else
76 {
77 if( _useLocalPosition )
78 _startValue = _target.localPosition;
79 else
80 _startValue = _target.position;
81 }
82
83 // validate the lookTarget if we are set to look at it
84 if( _lookAtType == GoLookAtType.TargetTransform )
85 {
86 if( _lookTarget == null )
87 _lookAtType = GoLookAtType.None;
88 }
89
90 // prep our smoothed rotation
91 _smoothedRotation = _target.rotation;
92 }
93
94
95 public override void tick( float totalElapsedTime )
96 {
97 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
98 var vec = _path.getPointOnPath( easedTime );
99
100 // if we are relative, add the vec to our startValue
101 if( _isRelative )
102 vec += _startValue;
103
104
105 // handle look types
106 switch( _lookAtType )
107 {
108 case GoLookAtType.NextPathNode:
109 {
110 _smoothedRotation.smoothValue = vec.Equals( _target.position ) ? Quaternion.identity : Quaternion.LookRotation( vec - _target.position );
111 _target.rotation = _smoothedRotation.smoothValue;
112 //var lookAtNode = ( _ownerTween.isReversed || _ownerTween.isLoopoingBackOnPingPong ) ? _path.getPreviousNode() : _path.getNextNode();
113 //_target.LookAt( lookAtNode, Vector3.up );
114 break;
115 }
116 case GoLookAtType.TargetTransform:
117 {
118 _target.LookAt( _lookTarget, Vector3.up );
119 break;
120 }
121 }
122
123
124 // assign the position
125 if( _useLocalPosition )
126 _target.localPosition = vec;
127 else
128 _target.position = vec;
129 }
130
131 }